package BanyuanClub.Study.Day0317;

import BanyuanClub.Work.Story.Five.Student;

import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class TextStream {
    public static void main(String[] args) {
        List<student> list = new ArrayList<>();
        list.add(new student("10001","aaa",'m',20));
        list.add(new student("10002","bbb",'f',19));
        list.add(new student("10003","ccc",'m',18));
        list.add(new student("10004","ddd",'f',23));
        list.add(new student("10005","eee",'m',22));
        list.add(new student("10006","fff",'f',17));
        List<student> idAge = new ArrayList<>();
        for (student stu:list
             ) {
            if (stu.getSex()=='m'){
                idAge.add(new student(stu.getID(),stu.getAge()));
            }
        }
        Collections.sort(idAge, (o1, o2) -> o2.getAge() - o1.getAge());
        List<String> ss = new ArrayList<>();
        for (student s : idAge
             ) {
            ss.add(s.getID());
        }
        System.out.println(ss);

        //计算男生年龄总和
        int i = list.stream().filter(s->s.getSex()=='m').mapToInt(student::getAge).sum();
        System.out.println(i);

        //按性别分组统计人数
//        Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(student::getSex, Collectors.summingInt(p -> 1)));
//        System.out.println(map);

        //求年龄最大的学生
        System.out.println("--------------------------------");
        student stu = list.stream().
                reduce((student, student2) ->
                        student.getAge() > student2.getAge() ? student:student2).get();
        System.out.println("---------"+stu+"-----------------");

        //流式写法  找出性别为男的人的ID并降序排序
        List<String> list1 = list.stream().
                //过滤出性别为男的
                filter(s->s.getSex()=='m').
                //根据年龄降序排序
                sorted(Comparator.comparing(student::getAge).reversed()).
                //将ID映射出来
                map(student::getID).
                //将映射出来的ID放到list中
                collect(Collectors.toList());
        System.out.println(list1);
    }

}

class student{
    private String ID;
    private String name;
    private char sex;
    private int age;

    public student(String ID, int age) {
        this.ID = ID;
        this.age = age;
    }

    public student(String ID, String name, char sex, int age) {
        this.ID = ID;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}